package com.higgins.eventtracking.client.health.fundamentals;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.action.DailyTab;
import com.higgins.eventtracking.client.health.action.FuelTab;
import com.higgins.eventtracking.client.health.components.HealthOverviewChart;
import com.higgins.eventtracking.client.health.components.HealthOverviewChartListBox;
import com.higgins.eventtracking.client.health.fundamentals.AchievementTab;
import com.higgins.eventtracking.client.health.fundamentals.DigestionTab;
import com.higgins.eventtracking.client.health.fundamentals.FrameOfMindTab;
import com.higgins.eventtracking.client.health.fundamentals.GoalsTab;
import com.higgins.eventtracking.client.health.fundamentals.HourlyTab;
import com.higgins.eventtracking.client.health.fundamentals.HygieneTab;
import com.higgins.eventtracking.client.health.fundamentals.NegativeTab;
import com.higgins.eventtracking.client.health.fundamentals.WellBeingTab;

public class TimetableTab extends VerticalPanel	implements SubTab	{
	private TabPanel tabPanel = new TabPanel();
	private YearlyTab yearlyTab = new YearlyTab();
	private MonthlyTab monthlyTab = new MonthlyTab();
	private WeeklyTab weeklyTab = new WeeklyTab();
	private DailyTab dailyTab = new DailyTab();
	private HourlyTab hourlyTab = new HourlyTab();
	private FuelTab fuelTab = new FuelTab();
	private boolean firstRun = true;
	private boolean includeFuel = true;
	private Integer tabSelected = null;
	
	public TimetableTab()	{	
		
	}
	public TimetableTab(boolean includeFuel)	{	
		this.includeFuel = includeFuel;
	}
   public TimetableTab(boolean includeFuel, Integer tabSelected)    {   
        this.includeFuel = includeFuel;
        this.tabSelected = tabSelected;
    }
	
	public void addGoalsTab()	{
		addTabs();
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		firstRun = false;
	}
	public void populateTabPanel()	{
		addTabs();
		add(tabPanel);
	}
	private void addTabs()	{
		tabPanel.add(yearlyTab, "Yearly");
		tabPanel.add(monthlyTab, "Monthly");
		tabPanel.add(weeklyTab, "Weekly");
		tabPanel.add(dailyTab, "Daily");
		tabPanel.add(hourlyTab, "Hourly");
		if (includeFuel)	{
			tabPanel.add(fuelTab, "Fuel");
		}
//		yearlyTab.update();
		Integer selectedTab = tabSelected!=null?tabSelected:0;
		tabPanel.selectTab(selectedTab);
		SelectionHandler<Integer> selectionHandler = createSelectionHandler();
		tabPanel.addSelectionHandler(selectionHandler);	
		
        if (selectedTab == 0)   {
            yearlyTab.update();
        }
        if (selectedTab == 1)   {
            monthlyTab.update();
        }
        if (selectedTab == 2)   {
            weeklyTab.update();
        }
        if (selectedTab == 3)   {
            dailyTab.update();
        }
        if (selectedTab == 4)   {
            hourlyTab.update();
        }
        if (selectedTab == 5)   {
            fuelTab.update();
        }
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	            if (integerSelectionEvent.getSelectedItem() == 0)	{
	            	yearlyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 1)	{
	            	monthlyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 2)	{
	            	weeklyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 3)	{
	            	dailyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 4)	{
	            	hourlyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 5)	{
	            	fuelTab.update();
	            }
	        }
		};
		return selectionHandler;
	}

}
